package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.PackageDao;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Package;
import com.itheima.service.PackageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service(interfaceClass = PackageService.class)
@Transactional
public class PackageServiceImpl implements PackageService {

    @Autowired
    private PackageDao packageDao;

    @Autowired
    private JedisPool jedisPool;

    private Logger logger = LoggerFactory.getLogger(SetmealServiceImpl.class);
    /**
     * 查询检查组信息
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {
        return packageDao.findAll();
    }


    /**
     * @author wu_zh
     * @date 2019/7/1 15:28
     * @param
     * @param packages
     * @param checkgroupIds
     * @return void
     * @Description 添加套餐
     */
    @Override
    public void add(Package packages, Integer[] checkgroupIds) {
        packageDao.add(packages);
        Integer setmeal_id = packages.getId();
        System.out.println(setmeal_id);
        if (checkgroupIds!=null&&checkgroupIds.length > 0){
            for (Integer checkgroupId : checkgroupIds) {
                packageDao.addByPackageAndCheckgroup(setmeal_id,checkgroupId);
            }
        }
        savePic2Redis(packages.getImg());
        jedisPool.getResource().flushDB();
        logger.debug("添加检套餐操作后清空redis");
    }

    @Override
    public PageInfo<Package> findPage(QueryPageBean queryPageBean) {
        if (!StringUtils.isEmpty(queryPageBean.getQueryString())){
            queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+"%");
        }

        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Package> packages = packageDao.findPage(queryPageBean.getQueryString());

        PageInfo pageInfo = new PageInfo(packages);
        return pageInfo;
    }

    //将图片名称保存到Redis
    public void savePic2Redis(String pic){
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,pic);
    }

}
